/**
 * Created by yanghaijin on 2018/6/25.
 */

const MsgCode = require('./../define/MsgCode');

let Room = require('./Room');

module.exports = class PokerRoom extends Room {
    constructor (app, options) {
        super(app, options);
        this._class_name = 'PokerRoom';
        this._uid2cards = {};
        this._uid2bet = {};
        this._uid2point = {};
    }

    /**
     * 消息处理
     * @param {Object} msg
     * @public
     * */
    onNotify (msg) {
        super.onNotify(msg);
        let code = msg['code'];
        let data = msg['data'];
        switch (code) {
            case MsgCode.OnBet:
                this._onBet(data);
                break;
            default:
                break;
        }
    }

    /**
     * 开始游戏
     * @protected
     * */
    _onStartGame () {
        super._onStartGame();
    }

    /**
     * 生成牌数据
     * @protected
     * */
    _generateCards () {
        this._uid2cards = {};
        this._uid2point = {};
        this._pokerGenerator.shuffle();
    }

    /**
     * 发牌
     * @protected
     * */
    _dealCards () {
        super._dealCards();
        this._generateCards();

        // 将洗好的牌推送给每个玩家
        let uids = Object.keys(this._uid2cards);
        let data = {};
        data.point = {};
        // data['deal_obj'] = {};
        data.deal_arr = [];
        for (let i = 0; i < uids.length; i++) {
            let uid = uids[i];
            if (this._uid2point.hasOwnProperty(uid)) {
                data.point[uid] = this._uid2point[uid];
            }
            data.deal_arr.push({uid: uid, card_arr: new Array(this._uid2cards[uid].length).fill(-1)});
        }

        // sendMessage
        for (let i = 0; i < data.deal_arr.length; i++) {
            let uid = data.deal_arr[i].uid;
            let original_arr = data.deal_arr[i].card_arr;
            data.deal_arr[i].card_arr = this._uid2cards[uid];
            this.send(MsgCode.DealCards, data, uid);
            data.deal_arr[i].card_arr = original_arr;
        }
    }

    /**
     * 获得庄家uid
     * @return {String | Number}
     * @protected
     * */
    _getBanker () {
        super._getBanker();
    }

    /**
     * 下注
     * @protected
     * */
    _bet () {
        this._uid2bet = {};
    }

    /**
     * 处理下注
     * @param {Object} data
     * @protected
     * */
    _onBet (data) {}

    /**
     * 操作
     * @protected
     * */
    _turn () {
        super._turn();
    }

    /**
     * 处理操作
     * @param {Object} data
     * @protected
     * */
    _onTurn (data) {
        super._onTurn(data);
    }

    /**
     * 结算
     * @protected
     * */
    _settlement () {
        super._settlement();
    }

    /**
     * 总结算
     * @protected
     * */
    _totalSettlement () {
        super._totalSettlement();
    }
};